﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import org.aswing.geom.*;
    import org.aswing.event.*;
    import org.aswing.graphics.*;
    import org.aswing.util.*;

    public class JPopup extends JRootPane {

        protected var ground_mc:Sprite;
        protected var modalMC:Sprite;
        protected var owner;
        protected var modal:Boolean;
        private var ownedEquipedPopups:ArrayList;
        private var lastLAF:Object;
        private var lastDragPos:IntPoint;

        public function JPopup(_arg1=null, _arg2:Boolean=false){
            if (_arg1 == null){
                _arg1 = AsWingManager.getRoot(false);
            };
            if ((((_arg1 is JPopup)) || ((_arg1 is DisplayObjectContainer)))){
                this.owner = _arg1;
            } else {
                if (_arg1 != null){
                    throw (new TypeError(((this + " JPopup's owner is not a DisplayObjectContainer or JPopup, owner is : ") + _arg1)));
                };
            };
            this.modal = _arg2;
            setName("JPopup");
            this.ownedEquipedPopups = new ArrayList();
            this.ground_mc = new Sprite();
            this.ground_mc.name = "ground_mc";
            this.ground_mc.visible = false;
            this.lastLAF = UIManager.getLookAndFeel();
            this.modalMC = new Sprite();
            this.initModalMC();
            this.ground_mc.addChild(this.modalMC);
            this.ground_mc.addChild(this);
            d_visible = false;
            addEventListener(Event.ADDED_TO_STAGE, this.__popupOpennedAddToList);
            addEventListener(Event.REMOVED_FROM_STAGE, this.__popupOfffromDisplayList);
        }
        public static function getPopups(_arg1:Stage=null):Array{
            if (_arg1 == null){
                _arg1 = AsWingManager.getStage();
            };
            var _local2:FocusManager = FocusManager.getManager(_arg1);
            return (_local2.getPopupsVector().toArray());
        }
        public static function getOwnedPopupsWithOwner(_arg1:DisplayObjectContainer):Array{
            var _local3:Array;
            var _local4:ArrayList;
            var _local5:int;
            var _local6:int;
            var _local7:JPopup;
            var _local2:FocusManager = FocusManager.getManager(_arg1.stage);
            if (_local2){
                _local3 = new Array();
                _local4 = _local2.getPopupsVector();
                _local5 = _local4.size();
                _local6 = 0;
                while (_local6 < _local5) {
                    _local7 = JPopup(_local4.get(_local6));
                    if (_local7.getOwner() === _arg1){
                        _local3.push(_local7);
                    };
                    _local6++;
                };
                return (_local3);
            };
            return ([]);
        }

        private function __popupOpennedAddToList(_arg1:Event):void{
            var _local2:FocusManager = FocusManager.getManager(stage);
            if (!_local2.getPopupsVector().contains(this)){
                _local2.getPopupsVector().append(this);
            };
            stage.addEventListener(Event.RESIZE, this.__resetModelMCWhenStageResized, false, 0, true);
        }
        private function __popupOfffromDisplayList(_arg1:Event):void{
            var _local2:FocusManager = FocusManager.getManager(stage);
            _local2.getPopupsVector().remove(this);
            stage.removeEventListener(Event.RESIZE, this.__resetModelMCWhenStageResized, false);
        }
        override public function isValidateRoot():Boolean{
            return (true);
        }
        override public function setEnabled(_arg1:Boolean):void{
            super.setEnabled(_arg1);
            this.ground_mc.mouseEnabled = isEnabled();
        }
        public function getOwner(){
            return (this.owner);
        }
        public function getPopupOwner():JPopup{
            return ((this.owner as JPopup));
        }
        public function getDisplayOwner():DisplayObjectContainer{
            return ((this.owner as DisplayObjectContainer));
        }
        public function changeOwner(_arg1):void{
            if (_arg1 == null){
                _arg1 = AsWingManager.getRoot(false);
            };
            if (this.owner != _arg1){
                this.owner = _arg1;
                if (this.isAddedToList()){
                    if (_arg1 == null){
                        throw (new Error("This popup is alreay on display list, can't be owned to null, please dispose it first."));
                    };
                    this.equipPopupContents();
                };
            };
        }
        public function setModal(_arg1:Boolean):void{
            if (this.modal != _arg1){
                this.modal = _arg1;
                this.modalMC.visible = this.modal;
                this.resetModalMC();
            };
        }
        public function isModal():Boolean{
            return (this.modal);
        }
        public function show():void{
            this.setVisible(true);
        }
        function getGroundContainer():DisplayObjectContainer{
            return (this.ground_mc);
        }
        override public function setVisible(_arg1:Boolean):void{
            if (((!((_arg1 == visible))) || (((_arg1) && (!(this.isAddedToList())))))){
                super.setVisible(_arg1);
                if (_arg1){
                    if (!this.isAddedToList()){
                        this.equipPopupContents();
                    };
                    this.resetModalMC();
                    dispatchEvent(new PopupEvent(PopupEvent.POPUP_OPENED));
                } else {
                    dispatchEvent(new PopupEvent(PopupEvent.POPUP_CLOSED));
                };
            };
            this.ground_mc.visible = this.shouldGroundVisible();
            if (_arg1){
                this.toFront();
            };
        }
        override public function pack():void{
            super.pack();
            revalidateIfNecessary();
        }
        private function isAddedToList():Boolean{
            return (!((this.ground_mc.parent == null)));
        }
        public function hide():void{
            this.setVisible(false);
        }
        public function dispose():void{
            var _local1:Stage;
            if (this.isAddedToList()){
                _local1 = stage;
                d_visible = false;
                this.disposeProcess(_local1);
                this.ground_mc.parent.removeChild(this.ground_mc);
                if (this.getPopupOwner() != null){
                    this.getPopupOwner().removeOwnedEquipedPopup(this);
                };
                dispatchEvent(new PopupEvent(PopupEvent.POPUP_CLOSED));
            };
        }
        protected function disposeProcess(_arg1:Stage):void{
        }
        function shouldGroundVisible():Boolean{
            var _local1:JPopup = this.getPopupOwner();
            if (_local1 != null){
                return (_local1.shouldOwnedPopupGroundVisible(this));
            };
            return (isVisible());
        }
        function shouldOwnedPopupGroundVisible(_arg1:JPopup):Boolean{
            return (_arg1.isVisible());
        }
        public function toBack():void{
            var _local1:JPopup;
            var _local2:int;
            if (((this.isAddedToList()) && (isVisible()))){
                _local1 = this.getPopupOwner();
                if (_local1 == null){
                    if (!DepthManager.isBottom(this.ground_mc)){
                        DepthManager.bringToBottom(this.ground_mc);
                    };
                } else {
                    _local2 = (_local1.parent.getChildIndex(_local1) + 1);
                    if (this.ground_mc.parent.getChildIndex(this.ground_mc) != _local2){
                        this.ground_mc.parent.setChildIndex(this.ground_mc, _local2);
                    };
                };
            };
        }
        public function toFront():void{
            if (((this.isAddedToList()) && (isVisible()))){
                if (!DepthManager.isTop(this.ground_mc)){
                    DepthManager.bringToTop(this.ground_mc);
                };
            };
        }
        override public function startDrag(_arg1:Boolean=false, _arg2:Rectangle=null):void{
            if (stage){
                super.startDrag(_arg1, _arg2);
                stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.__dragMoving);
                stage.addEventListener(MouseEvent.MOUSE_MOVE, this.__dragMoving, false, 0, true);
                this.lastDragPos = getLocation();
            };
        }
        override public function stopDrag():void{
            super.stopDrag();
            if (stage){
                stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.__dragMoving);
            };
        }
        private function __dragMoving(_arg1:MouseEvent):void{
            bounds.x = d_x;
            bounds.y = d_y;
            var _local2:IntPoint = getLocation();
            if (!_local2.equals(this.lastDragPos)){
                dispatchEvent(new MovedEvent(this.lastDragPos, _local2));
            };
            this.lastDragPos = _local2;
        }
        public function getOwnedPopups():Array{
            return (getOwnedPopupsWithOwner(this));
        }
        public function getOwnedEquipedPopups():Array{
            return (this.ownedEquipedPopups.toArray());
        }
        public function getPopupAncestorMC():DisplayObjectContainer{
            var _local1:JPopup = this;
            while (_local1.getPopupOwner() != null) {
                _local1 = _local1.getPopupOwner();
            };
            return (_local1.getDisplayOwner());
        }
        public function getModalMC():Sprite{
            return (this.modalMC);
        }
        public function resetModalMC():void{
            if (!this.isModal()){
                this.modalMC.width = 0;
                this.modalMC.height = 0;
                this.modalMC.visible = false;
                return;
            };
            this.modalMC.visible = true;
            var _local1:IntRectangle = AsWingUtils.getVisibleMaximizedBounds(this.ground_mc);
            this.modalMC.width = (_local1.width + 200);
            this.modalMC.height = (_local1.height + 200);
            this.modalMC.x = (_local1.x - 100);
            this.modalMC.y = (_local1.y - 100);
        }
        protected function initModalMC():void{
            this.modalMC.tabEnabled = false;
            this.modalMC.visible = this.modal;
            this.modalMC.graphics.clear();
            var _local1:ASColor = new ASColor(0, 0);
            var _local2:Graphics2D = new Graphics2D(this.modalMC.graphics);
            _local2.fillRectangle(new SolidBrush(_local1), 0, 0, 1, 1);
        }
        private function addOwnedEquipedPopup(_arg1:JPopup):void{
            this.ownedEquipedPopups.append(_arg1);
        }
        private function removeOwnedEquipedPopup(_arg1:JPopup):void{
            this.ownedEquipedPopups.remove(_arg1);
        }
        private function __resetModelMCWhenStageResized(_arg1:Event):void{
            if (isVisible()){
                this.resetModalMC();
            };
        }
        private function equipPopupContents():void{
            var _local1:JPopup;
            var _local2:DisplayObjectContainer;
            if ((this.owner is JPopup)){
                _local1 = JPopup(this.owner);
                _local1.ground_mc.addChild(this.ground_mc);
                _local1.addOwnedEquipedPopup(this);
            } else {
                if ((this.owner is DisplayObjectContainer)){
                    _local2 = DisplayObjectContainer(this.owner);
                    _local2.addChild(this.ground_mc);
                } else {
                    throw (new TypeError(((this + " JPopup's owner is not a mc or JPopup, owner is : ") + this.owner)));
                };
            };
            if (this.lastLAF != UIManager.getLookAndFeel()){
                AsWingUtils.updateChildrenUI(this);
                this.lastLAF = UIManager.getLookAndFeel();
            };
        }

    }
}//package org.aswing 
